package net.qhzw.vip.qiyu.task;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.dto.DispatchDto;
import net.qhzw.vip.qiyu.entity.MaintRpt;
import net.qhzw.vip.qiyu.entity.ProcessNotice;
import net.qhzw.vip.qiyu.mapper.MaintRptMapper;
import net.qhzw.vip.qiyu.service.MailService;
import net.qhzw.vip.qiyu.service.MaintRptService;
import net.qhzw.vip.qiyu.service.ProcessNoticeService;
import net.qhzw.vip.qiyu.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class OrderReminderTask {

    @Autowired
    private MaintRptService maintRptService;

    @Autowired
    private MailService mailService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private MaintRptMapper maintRptMapper;

    @Autowired
    private ProcessNoticeService processNoticeService;


    /***
     * 派单提醒
     */
    @Scheduled(cron = "0/30 * * * * ?")
    public void execution() {
        log.info("派单提醒, 时间: {}", LocalDateTime.now());
        LambdaQueryWrapper<MaintRpt> queryWrapper = new LambdaQueryWrapper<MaintRpt>()
                .eq(MaintRpt::getStatus, 1);
        int count = maintRptService.count(queryWrapper);
        if (count > 0){
            List<MaintRpt> list = maintRptService.list();
            for (MaintRpt maintRpt : list) {
                Long id = maintRpt.getId();
                Object cacheObject = redisUtils.getCacheObject("urgentNotice:" + id);
                if (cacheObject != null){
                    return;
                }
                Date created = maintRpt.getCreated();
                //发送通知邮件
                send(id, created);
            }
        }
    }


    public void send(Long id, Date created) {
        redisUtils.setCacheObject("urgentNotice:"+id, id, 20, TimeUnit.MINUTES);
        Long timeLong = created.getTime() + 1000 * 60 * 10 ;
        String format = DateUtil.format(new Date(timeLong), "yyyy-MM-dd HH:mm:ss");
        MaintRpt maintRpt = maintRptMapper.selectUrgentDispatchNotice(id, format);
        if (maintRpt == null){
            return;
        }
        try {
            List<ProcessNotice> processNotice = getProcessNotice();
            if (!CollectionUtils.isEmpty(processNotice)){
                for (ProcessNotice notice : processNotice) {
                    mailService.sendHtmlMail(notice.getEmail(), new DispatchDto(maintRpt, notice.getName()), "urgentNotice");
                }
            }
        }catch (Exception e){
            log.error("派单异常：{}", JSON.toJSONString(e, true));
        }

    }


    /**
     *
     * 获取紧急通知联系人
     */
    private List<ProcessNotice> getProcessNotice(){
        List<ProcessNotice> list = processNoticeService.list(new LambdaQueryWrapper<ProcessNotice>()
                .eq(ProcessNotice::getStatus, 1));
        return list;
    }




}
